import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';
import { MockParams } from '@/types/mock';
import { userData as tableData, markData } from './projectJsonData';
import type { DataEnum } from '@/api/project/project.d';

setupMock({
    setup() {
        // 用户列表
        Mock.mock(new RegExp('/api/user/search'), (data: MockParams) => {
            const { keyword, companyId, status, page, pageSize } = JSON.parse(data.body || '{}');
            if (keyword) {
                if (keyword.indexOf('1') !== -1) {
                    const newRes = tableData.filter(item => item.mobile === keyword);
                    return successResponseWrap({
                        total: newRes.length,
                        pages: newRes.length <= 10 ? 1 : 10,
                        list: newRes
                    })
                } else {
                    const newRes = tableData.filter(item => item.userName === keyword);
                    return successResponseWrap({
                        total: newRes.length,
                        pages: newRes.length <= 10 ? 1 : 10,
                        list: newRes
                    })
                }
            }
            if (companyId) {
                return successResponseWrap({
                    total: tableData.length,
                    pages: tableData.length <= 10 ? 1 : 10,
                    list: tableData
                })
            }
            return successResponseWrap({
                total: tableData.length,
                pages: tableData.length <= 10 ? 1 : 10,
                list: tableData
            })
        })

        // 单位列表
        Mock.mock(new RegExp('/api/company/list'), (data: MockParams) => {
            const { status } = JSON.parse(data.body || '{}');
            return successResponseWrap({
                total: markData.length,
                pages: markData.length <= 10 ? 1 : 10,
                list: markData
            })
        })

        Mock.mock(new RegExp('/api/company'), (data: MockParams) => {
            if (data.type === 'POST') {
                // 新增单位
                const { name, remark } = JSON.parse(data.body || '{}');
                if (name) {
                    markData.push({
                        id: markData.length,
                        name,
                        remark: remark === '' ? null : remark,
                        status: 1
                    })
                    return successResponseWrap(null);
                }
                return failResponseWrap(null, '创建失败', 500);
            } else if (data.type === 'PUT') {
                // 编辑单位状态
                const { id, status } = JSON.parse(data.body || '{}');
                let res;
                markData.map(item => {
                    if (item.id === id) {
                        item.status = status;
                        res = successResponseWrap(null);
                    }
                })
                return res;
            }
        })
    }
})